//
//  TopicViewControllert.m
//  lisure0.1
//
//  Created by xalo on 16/4/21.
//  Copyright © 2016年 Mr.wangYongxu. All rights reserved.
//

#import "TopicViewControllert.h"
#import "TopicModel.h"
#import "authOrUsrModel.h"
#import "CounterListModel.h"
#import "TopicTableViewCell.h"
#import "TopicDetailViewController.h"


#define kTopicUrl @"http://api2.pianke.me/group/posts_hotlist"

@interface TopicViewControllert ()<UITableViewDelegate, UITableViewDataSource>

@property(nonatomic, retain)NSMutableArray* modelDataSourceArr;
@property(nonatomic, assign)NSInteger total;//标记，总共的数量
@property (weak, nonatomic) IBOutlet UITableView *tableView;

@end

@implementation TopicViewControllert

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Left"
                                                                             style:UIBarButtonItemStylePlain
                                                                            target:self
                                                                            action:@selector(presentLeftMenuViewController:)];
    [self fetchDataWithUrl:kTopicUrl parameter:nil];
    self.automaticallyAdjustsScrollViewInsets = NO;
    [self.view layoutIfNeeded];
    [self.tableView setBackgroundView:[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"MenuBackground"]]];
    self.tableView.backgroundColor = [UIColor clearColor];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(NSMutableArray *)modelDataSourceArr{

    if (!_modelDataSourceArr) {
        _modelDataSourceArr = [NSMutableArray array];
    }
    return _modelDataSourceArr;
}

#pragma mark - <UItableViewDelegate,UItableViewDataSource>

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    TopicTableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:@"TopicTableViewCell" forIndexPath:indexPath];
//    [cell layoutIfNeeded];
    [cell setCellWithModel:self.modelDataSourceArr[indexPath.row]];
    cell.backgroundColor = [UIColor clearColor];
    
        return cell;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    return [tableView showMessage:@"等待刷新" byDataSourceCount:self.modelDataSourceArr.count];
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

    return 200;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    TopicDetailViewController* detail = [[UIStoryboard storyboardWithName:@"Topic" bundle:nil] instantiateViewControllerWithIdentifier:@"TopicDetailViewController"];
    detail.model = self.modelDataSourceArr[indexPath.row];
    [self.navigationController pushViewController:detail animated:YES];

}
#pragma mark - AFNetworking

-(void)fetchDataWithUrl:(NSString*)url parameter:(NSDictionary*)dic{

    AFHTTPSessionManager* manager = [AFHTTPSessionManager manager];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    [manager POST:url parameters:dic progress:^(NSProgress * _Nonnull uploadProgress) {

    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSDictionary* dic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingAllowFragments error:nil];
        self.total = [dic[@"data"][@"total"] integerValue];
        NSArray* array = dic[@"data"][@"list"];
        for (int i = 0; i < array.count; i++) {

            TopicModel* model = [[TopicModel alloc] init];
            [model setValuesForKeysWithDictionary:array[i]];
            [self.modelDataSourceArr addObject:model];
        }
        [self.tableView reloadData];
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {

    }];
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
